package com.mspbots.core.wise.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mspbots.core.wise.model.TenantTicketReject;
import com.mspbots.core.wise.service.SendMailService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import java.util.Map;

/**
 * @author Dinnes Zhang
 * @date
 */
@Log4j2
@Service
public class SendMailServiceImpl implements SendMailService {

    private final JavaMailSender mailSender;
    protected ObjectMapper objectMapper;


    public SendMailServiceImpl(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

    @Autowired
    public void setObjectMapper(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    /**
     * Add the detailed information of the ticket to help the troubleshooting. Whatever the technician can see on the screen:
     * <p>
     * Also, send email to users
     * Admin can add CC
     * Daniel in BCC
     * Ticket summary information, don't include the description and notes.
     *
     * @param params
     * @return
     */

    @Override
    public Boolean generateMail(TenantTicketReject params) {
        Object toObj = params.getTo();
        Object ccObj = params.getCc();
        Object bccObj = params.getBcc();
        if (ObjectUtils.isEmpty(params.getFrom())) {
            params.setFrom("support@mspbots.ai");
        }

        log.info("to:{}", toObj);
        log.info("cc:{}", ccObj);
        log.info("bcc:{}", bccObj);

        String[] to = ObjectUtils.isEmpty(toObj) ? null : toObj.toString().split(",");
        String[] cc = ObjectUtils.isEmpty(ccObj) ? null : ccObj.toString().split(",");
        String[] bcc = ObjectUtils.isEmpty(bccObj) ? null : bccObj.toString().split(",");


        MimeMessagePreparator preparator = mimeMessage -> {
            if (to != null) {
                InternetAddress[] toAddress = new InternetAddress[to.length];
                for (int i = 0; i < to.length; i++) {
                    toAddress[i] = new InternetAddress(to[i]);
                }
                mimeMessage.addRecipients(Message.RecipientType.TO, toAddress);
            }
            if (cc != null) {
                InternetAddress[] ccAddress = new InternetAddress[cc.length];
                for (int i = 0; i < cc.length; i++) {
                    ccAddress[i] = new InternetAddress(cc[i]);
                }
                mimeMessage.addRecipients(Message.RecipientType.CC, ccAddress);
            }
            if (bcc != null) {
                InternetAddress[] bccAddress = new InternetAddress[bcc.length];
                for (int i = 0; i < bcc.length; i++) {
                    bccAddress[i] = new InternetAddress(bcc[i]);
                }
                mimeMessage.addRecipients(Message.RecipientType.BCC, bccAddress);
            }
            mimeMessage.setFrom(new InternetAddress(params.getFrom()));
            mimeMessage.setSubject(params.getSubject());

            mimeMessage.setContent(params.getContent(),
                    "text/html;charset=UTF-8");
        };

        try {
            this.mailSender.send(preparator);
        } catch (MailException ex) {
            log.error("mail send error");
            return false;
        }
        return true;
    }

    @Override
    public Boolean send(Map<String, Object> params) {
        //        String receiver = "1785319835@qq.com";
        String receiver = params.get("email").toString();
//        String receiverCc = "ken.lee@jexet.com";

        MimeMessagePreparator preparator = mimeMessage -> {
            mimeMessage.setRecipient(Message.RecipientType.TO,
                    new InternetAddress(receiver));
//            mimeMessage.setRecipients(Message.RecipientType.CC,
//                    new Address[]{new InternetAddress(receiverCc)});
            mimeMessage.setFrom(new InternetAddress("support@mspbots.ai"));
            mimeMessage.setSubject(params.get("subject").toString());

            mimeMessage.setContent(params.get("content"),
                    "text/html;charset=UTF-8");
        };

        try {
            this.mailSender.send(preparator);
        } catch (MailException ex) {
            log.error("mail send error");
            return false;
        }
        return true;
    }

}
